from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any, Union
from datetime import datetime, date

# 通用响应模型
class Response(BaseModel):
    code: int = 200
    message: str = "成功"
    data: Optional[Any] = None

# 商户相关模型
class MerchantBase(BaseModel):
    name: str
    code: str
    contact_person: Optional[str] = None
    contact_phone: Optional[str] = None
    address: Optional[str] = None
    status: int = 1
    remark: Optional[str] = None

class MerchantCreate(MerchantBase):
    pass

class MerchantUpdate(MerchantBase):
    name: Optional[str] = None
    code: Optional[str] = None

class Merchant(MerchantBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 报告相关模型
class ReportBase(BaseModel):
    merchant_id: int
    report_name: str
    report_date: date
    expected_file_count: int = 2
    remark: Optional[str] = None

class ReportCreate(ReportBase):
    pass

class ReportUpdate(BaseModel):
    report_name: Optional[str] = None
    expected_file_count: Optional[int] = None
    remark: Optional[str] = None

class Report(ReportBase):
    id: int
    report_code: str
    status: int
    file_count: int
    operator: Optional[str] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 文件上传相关模型
class FileUploadBase(BaseModel):
    merchant_id: int
    report_id: int
    file_name: str
    file_size: int
    file_type: int
    file_format: str
    store_type: int = 1
    remark: Optional[str] = None

class FileUploadCreate(FileUploadBase):
    file_path: Optional[str] = None
    file_content: Optional[bytes] = None
    operator: Optional[str] = None

class FileUpload(FileUploadBase):
    id: int
    file_path: Optional[str] = None
    status: int
    process_status: int
    process_message: Optional[str] = None
    operator: Optional[str] = None
    upload_time: datetime
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 整合报告相关模型
class IntegratedReportBase(BaseModel):
    report_id: int
    file_size: int
    file_format: str
    file_name: str
    store_type: int = 1
    remark: Optional[str] = None

class IntegratedReportCreate(IntegratedReportBase):
    file_path: Optional[str] = None
    report_content: Optional[bytes] = None
    operator: Optional[str] = None

class IntegratedReport(IntegratedReportBase):
    id: int
    file_path: Optional[str] = None
    integration_time: datetime
    status: int
    operator: Optional[str] = None
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

# 文件列表返回模型
class FileListItem(BaseModel):
    fileId: int
    reportId: int
    merchantId: int
    merchantName: str
    fileName: str
    fileType: int
    fileSize: int
    storeType: int
    status: int
    processStatus: int
    uploadTime: str
    operator: Optional[str] = None

class FileListResponse(BaseModel):
    total: int
    pageNum: int
    pageSize: int
    list: List[FileListItem]

# 报告列表返回模型
class ReportListItem(BaseModel):
    reportId: int
    reportCode: str
    merchantId: int
    merchantName: str
    reportName: str
    reportDate: str
    status: int
    fileCount: int
    expectedFileCount: int
    createTime: str

class ReportListResponse(BaseModel):
    total: int
    pageNum: int
    pageSize: int
    list: List[ReportListItem]

# 商户列表返回模型
class MerchantListItem(BaseModel):
    merchantId: int
    name: str
    code: str
    contactPerson: Optional[str] = None
    contactPhone: Optional[str] = None
    status: int
    createTime: str

class MerchantListResponse(BaseModel):
    total: int
    pageNum: int
    pageSize: int
    list: List[MerchantListItem]

# 报告详情返回的文件项
class ReportFileItem(BaseModel):
    fileId: int
    fileName: str
    fileType: int
    fileSize: int
    storeType: int
    uploadTime: str
    operator: Optional[str] = None

# 报告详情返回的整合报告项
class ReportIntegratedItem(BaseModel):
    id: int
    fileName: str
    fileSize: int
    integrationTime: str

# 报告详情模型
class ReportDetail(BaseModel):
    reportId: int
    reportCode: str
    merchantId: int
    merchantName: str
    reportName: str
    reportDate: str
    status: int
    fileCount: int
    expectedFileCount: int
    createTime: str
    files: List[ReportFileItem]
    integratedReport: Optional[ReportIntegratedItem] = None 